package org.jeecg.modules.zhima.portal.controller;

import org.jeecg.modules.zhima.base.common.CommonPage;
import org.jeecg.modules.zhima.base.common.Result;
import org.jeecg.modules.zhima.center.entity.OmsOrder;
import org.jeecg.modules.zhima.portal.domain.OmsOrderDetail;
import org.jeecg.modules.zhima.portal.domain.OrderParam;
import org.jeecg.modules.zhima.portal.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单管理Controller
 * Created by dingzg on 2018/8/30.
 */
@Controller
@Api(tags = "OmsPortalOrderController", description = "订单管理")
@RequestMapping("/order")
public class MOmsOrderController {
    @Autowired
    private IOrderService orderService;

    @ApiOperation("根据购物车信息生成订单")
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ResponseBody
    public Result generateOrder(@RequestBody OrderParam orderParam) {
        Map<String, Object> result = orderService.generateOrder(orderParam);
        return Result.success(result);
    }

//    @ApiOperation("用户支付成功的回调")
//    @RequestMapping(value = "/paySuccess", method = RequestMethod.POST)
//    @ResponseBody
//    public Result paySuccess(@RequestParam Long orderId,@RequestParam Integer payType) {
//        Integer count = portalOrderService.paySuccess(orderId,payType);
//        return Result.success(count);
//    }
//
//    @ApiOperation("自动取消超时订单")
//    @RequestMapping(value = "/cancelTimeOutOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public Result cancelTimeOutOrder() {
//        portalOrderService.cancelTimeOutOrder();
//        return Result.ok();
//    }
//
//    @ApiOperation("取消单个超时订单")
//    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public Result cancelOrder(Long orderId) {
//        portalOrderService.sendDelayMessageCancelOrder(orderId);
//        return Result.ok();
//    }

    @ApiOperation("按状态分页获取用户订单列表")
    @ApiImplicitParam(name = "status", value = "订单状态：-1->全部；0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭",
            defaultValue = "-1", allowableValues = "-1,0,1,2,3,4", paramType = "query", dataType = "int")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list(@RequestParam Integer status,
                                                   @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                   @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        CommonPage<OmsOrderDetail> orderPage = orderService.list(status, pageNo, pageSize);
        return Result.success(orderPage);
    }

    @ApiOperation("根据ID获取订单详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public Result detail(@RequestParam Integer orderId) {
        OmsOrderDetail orderDetail = orderService.detail(orderId);
        return Result.success(orderDetail);
    }

    @ApiOperation("用户取消订单")
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ResponseBody
    public Result cancelUserOrder(@RequestBody OrderParam param) {
        orderService.cancelOrder(param.getOrderId());
        return Result.ok();
    }

    @ApiOperation("用户确认收货")
    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    @ResponseBody
    public Result confirmReceiveOrder(@RequestBody OrderParam param) {
        orderService.confirmReceiveOrder(param.getOrderId());
        return Result.ok();
    }

    @ApiOperation("用户删除订单")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteOrder(@RequestBody OrderParam param) {
        orderService.deleteOrder(param.getOrderId());
        return Result.ok();
    }

    @ApiOperation("修改收货地址")
    @RequestMapping(value = "/addressUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Result addressUpdate(@RequestBody OmsOrder order) {
        return Result.ok(orderService.addressUpdate(order));
    }
}
